#include <iostream>
#include <pthread.h>
#include <vector>
#include <ctime>
#include <cstring>
#include <sys/types.h>
#include <unistd.h>

using namespace std;

class data
{
public:
    int x;
    int y;
    char op;
};

class res
{
public:
    int code;
    int result;
};

const char* str = "+-*/%";

class inAndOut
{
public:
    inAndOut()
    {
        datas.resize(10);
        results.resize(10);
        for (int i = 0; i < 10; i++)
        {
            datas[i].x = (rand() ^ getpid()) % 100;
            datas[i].y = (rand() ^ getpid()) % 100;
            datas[i].op = str[(rand() ^ getpid()) % strlen(str)];
        }
    }
public:
    vector<data> datas;
    vector<res> results;
};

void *threadRoutine(void *args)
{
    inAndOut *r = static_cast<inAndOut *>(args);
    for (int i = 0; i < 10; i++)
    {
        int x = r->datas[i].x;
        int y = r->datas[i].y;
        char op = r->datas[i].op;
        r->results[i].code = 0;
        switch (op)
        {
        case '+':
            r->results[i].result = x + y;
            break;
        case '-':
            r->results[i].result = x - y;
            break;
        case '*':
            r->results[i].result = x * y;
            break;
        case '/':
            if (r->datas[i].y == 0)
                r->results[i].code = -1;
            r->results[i].result = x / y;
            break;
        case '%':
            r->results[i].result = x % y;
            break;
        }
    }
    return (void *)r;
}

int main()
{
    srand(time(nullptr));
    inAndOut obj;

    // 1.线程创建
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, &obj);

    // 2.线程等待
    void *ret;
    pthread_join(tid, &ret);
    inAndOut *r = static_cast<inAndOut *>(ret);
    for (int i = 0; i < 10; i++)
    {
        if(r->results[i].code == -1)
            cout << "SIGFPE exception" << endl;
        else    
            cout << r->datas[i].x << r->datas[i].op << r->datas[i].y << "=" << r->results[i].result << endl;
    }
    return 0;
}